package com.cz.ui.chat;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.cz.R;
import com.cz.adapter.chat.ChatViewListAdapter;
import com.cz.pojo.Chat;
import com.cz.pojo.Dialogue;
import com.cz.utils.DBOpenHelper;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ChatInfo  extends AppCompatActivity {

    private ArrayList<Chat> chats;
    private ListView chatList;
    private ChatViewListAdapter chatViewListAdapter;
    private Button sendBut;
    private EditText input;
    private DBOpenHelper dbOpenHelper;
    private String nickName;
    private SQLiteDatabase db;
    private String headimg;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        Intent intent = getIntent();
        nickName = intent.getStringExtra("nickName");
        chatList = this.findViewById(R.id.listView_chat);
        sendBut = this.findViewById(R.id.listView_send);

        showInfo();


        //消息发送监听
        sendBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input = findViewById(R.id.listView_input);
                String s = input.getText().toString();
                //向数据库存放数据
                if(s!=""){
                    updateDB(s);
                }
                input.setText("");
            }
        });

        chatViewListAdapter = new ChatViewListAdapter(this, chats);
        chatList.setAdapter(chatViewListAdapter);
    }

    //发送消息后使数据持久化
    private void updateDB(String massage) {
        dbOpenHelper = new DBOpenHelper(this,"WeChatMini",null,2);
        db = dbOpenHelper.getReadableDatabase();

        //1 向消息数据库存放消息数据

        //获取系统时间
        Date date = new Date();
        DateFormat bf = new SimpleDateFormat("HH:mm");
        String format = bf.format(date);


        int rid = 0;
        Cursor user = db.rawQuery("select * from chatrecords", null);
        while(user.moveToNext()) {
            rid = user.getShort(0);
        }

        insertChatrecords(db,new Chat(rid+1,massage,nickName,1,format));
        chats.add(new Chat(rid+1,massage,nickName,1,format));

        //2 向消息列表修改最后的聊天
        Cursor user2 = db.query("dialoguelist", null, "userid=?",
                new String[]{nickName}, null, null, null);
        //2.1 首先查看是否存在数据库
        //2.3 存在修改
        if(user2.moveToNext()){
            System.out.println("存在");

            ContentValues newValue=new ContentValues();
            newValue.put("endmessage",massage);
            newValue.put("endtime",format);
            int result=db.update("dialoguelist",newValue,"userid=?",new String[]{nickName});

            System.out.println(result);
        }else{
            //2.2 不存在添加
            int listid = 0;
            Cursor user3 = db.rawQuery("select * from dialoguelist", null);
            while(user3.moveToNext()) {
                listid = user3.getShort(0);
            }
            String headimg = selectHeadImg(db);
            Dialogue dialogue = new Dialogue(listid+1,massage,nickName,headimg,format);

            insertDialoguelist(db,dialogue);
        }
        db.close();
    }

    //渲染聊天界面
    private void showInfo() {

        //好友信息申请并且渲染
        Log.i("需要查询的微信号是: ",nickName);

        //数据库操作
        dbOpenHelper = new DBOpenHelper(this,"WeChatMini",null,2);
        db = dbOpenHelper.getReadableDatabase();
        //满足条件查询
        Cursor user = db.query("chatrecords", null, "userid=?",
                new String[]{nickName}, null, null, null);

        String headimg = selectHeadImg(db);

        //查出头像


        insertInfo0(db);
        insertInfo1(db);
        insertInfo2(db);
        insertInfo3(db);
        insertInfo4(db);

        chats = new ArrayList<>();
        while(user.moveToNext()) {
            //根据查出来的数据封装成好友对象
            int rid = Integer.parseInt(user.getString(0));
            String message = user.getString(1);
            String userid = user.getString(2);
            int ismyself =  Integer.parseInt(user.getString(3));
            String msgtime = user.getString(4);
            Chat chat = new Chat(rid, message, userid, ismyself, msgtime);
            chats.add(chat);
        }

        db.close();
    }

    protected String selectHeadImg(SQLiteDatabase db){
        Cursor friends = db.query("friends", null, "nickname=?",
                new String[]{nickName}, null, null, null);
        String headimg = null;
        //查出头像
        while(friends.moveToNext()) {
            if(headimg==null){
                headimg= friends.getString(4);
                System.out.println(headimg);
                break;
            }
        }
        return headimg;
    }

    //向chatrecords表中插入聊天记录
    private void insertChatrecords(SQLiteDatabase db,Chat chat) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("rid",chat.getRid());
        contentValues.put("message",chat.getMassage());
        contentValues.put("userid",chat.getUserid());
        contentValues.put("ismyself",chat.getIsmyself());
        contentValues.put("msgtime",chat.getMsgtime());
        long mine = db.insert("chatrecords",null, contentValues);
    }

    //向dialoguelist插入聊天最后事件
    private void insertDialoguelist(SQLiteDatabase db,Dialogue dialogue) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("listid",dialogue.getListid());
        contentValues.put("endmessage",dialogue.getEndmessage());
        contentValues.put("userid",dialogue.getUserid());
        contentValues.put("headimg",dialogue.getHeadimg());
        contentValues.put("endtime",dialogue.getEndtime());
        long mine = db.insert("dialoguelist",null, contentValues);
    }


    //用于结局intent传值问题
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo0(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("rid",0);
        contentValues.put("message","今早上吃啥");
        contentValues.put("userid","BiShuGui");
        contentValues.put("ismyself",0);
        contentValues.put("msgtime","16:30");
        long mine = db.insert("chatrecords",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo1(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("rid",1);
        contentValues.put("message","鸡公煲?");
        contentValues.put("userid","BiShuGui");
        contentValues.put("ismyself",0);
        contentValues.put("msgtime","16:30");
        long mine = db.insert("chatrecords",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo2(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("rid",2);
        contentValues.put("message","不知道啊");
        contentValues.put("userid","BiShuGui");
        contentValues.put("ismyself",1);
        contentValues.put("msgtime","16:31");
        long mine = db.insert("chatrecords",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo3(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("rid",3);
        contentValues.put("message","哪去食堂看看怎么样");
        contentValues.put("userid","BiShuGui");
        contentValues.put("ismyself",0);
        contentValues.put("msgtime","16:31");
        long mine = db.insert("chatrecords",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo4(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("rid",4);
        contentValues.put("message","好好好好好好好好好好好好好好好好好好好好好");
        contentValues.put("userid","BiShuGui");
        contentValues.put("ismyself",1);
        contentValues.put("msgtime","16:32");
        long mine = db.insert("chatrecords",null, contentValues);
    }


}